<?php
namespace App\Http\Controllers\Admin;
use App\Models\Admin\TvmMobileDetail;
use App\Models\Admin\TvmMobileCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Admin\AdminAction;
use App\Models\Admin\basiconfig;
use App\Models\Admin\TransferReason;



class BasiconfigController extends CommonController {

	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
	}





	/**
	 * 转账规则
	 **/
	public function transfer_rule(Request $request) {
		$condition = array();
		$state = $request->input('state');
		if(!empty($state)){
			$state = explode('_', $state);
		}
		$from_ownertype = $request->input('from_ownertype');
		if(!empty($from_ownertype)){
			$from_ownertype = explode('_', $from_ownertype);
		}
		$from_pursetype = $request->input('from_pursetype');
		if(!empty($from_pursetype)){
			$from_pursetype = explode('_', $from_pursetype);
		}
		$to_ownertype = $request->input('to_ownertype');
		if(!empty($to_ownertype)){
			$to_ownertype = explode('_', $to_ownertype);
		}
		$to_pursetype = $request->input('to_pursetype');
		if(!empty($to_pursetype)){
			$to_pursetype = explode('_', $to_pursetype);
		}
		if(!empty($state)){
			$condition[] = ['state','=',$state[0]];
		}
		if(!empty($from_ownertype)){
			$condition[] = ['from_ownertype','=',$from_ownertype[0]];
		}
		if(!empty($from_pursetype)){
			$condition[] = ['from_pursetype','=',$from_pursetype[0]];
		}
		if(!empty($to_ownertype)){
			$condition[] = ['to_ownertype','=',$to_ownertype[0]];
		}
		if(!empty($to_pursetype)){
			$condition[] = ['to_pursetype','=',$to_pursetype[0]];
		}
	
		
		
		//查询转账规则列表
			$data = DB::table('tblc_transfer_rule')->where($condition)->paginate(20);
		
		//展示模板
		return view('admin.basiconfig.transfer_rule',[
			'data' => $data,
			'state' => $state[1],
			'from_ownertype' => $from_ownertype[1],
			'from_pursetype' => $from_pursetype[1],
			'to_ownertype' => $to_ownertype[1],
			'to_pursetype' => $to_pursetype[1]
		]);
	}


	/**
	 * 转账原因
	 **/
	public function transfer_reason(Request $request) {
		$condition = array();
		$status = $request->input('status');
		if(!empty($status)){
			$status = explode('_', $status);
		}
		if(!empty($status)){
			$condition[] = ['status','=',$status[0]];
		}
		$reson_name = $request->input('reson_name');
		if(!empty($reson_name)){
			$condition[] = ['reson_name', 'like', '%'.$reson_name.'%'];
		}
		$reson_code = $request->input('reson_code');
		if(!empty($reson_code)){
			$condition[] = ['reson_code','=',$reson_code];
		}
		$business_id = $request->input('business_id');
		if(!empty($business_id)){
			$condition[] = ['business_id','=',$business_id];
		}
		
		
		//查询转账规则原因
			$data = DB::table('tblc_transfer_reson')->where($condition)->paginate(20);

		//展示模板
		return view('admin.basiconfig.transfer_reason',[
			'data' => $data,
			'status' => $status[1],
			'reson_name' => $reson_name,
			'reson_code' => $reson_code,
			'business_id' => $business_id
		]);
	}

	/**
	 * add转账规则
	 **/
	public function add_rule(Request $request) {
		     	$id = $request->input('ruleid');
		    	if($request->isMethod('post')){
				$from_ownertype = $request->input('from_ownertype');
				if(!$from_ownertype){
					$this->ajax_error('请选择转出方类型');
				}

				$data = array(
					'ruleid' => Basiconfig::getNextSeq(),
					'from_ownertype' => $request->input('from_ownertype'), //转出方类型
					'from_pursetype' => $request->input('from_pursetype'), //转出方钱包类型
					'to_ownertype' => $request->input('to_ownertype'), //接收方类型
					'to_pursetype' => $request->input('to_pursetype'), //接收方钱包类型
					'state' => $request->input('state'), //状态
					'same_ownerid' => $request->input('same_ownerid'),//是否
					'same_subid' => $request->input('same_subid'),//是否
					'remark' => $request->input('remark'), //备注

				);
				if(!$id){

					$result = Db::table('tblc_transfer_rule')->insert($data);
					if(!$result){
						$this->ajax_error('添加失败');
					}
						// record_action(AdminAction::getNextSeq(),'rbac','menuAdd','菜单信息添加',json_encode($_POST));
						$this->ajax_success('添加成功');
				}else{
					$data['ruleid'] = $id;
					$result1 = Db::table('tblc_transfer_rule')->where('ruleid',$id)->update($data);
					if($result1){
						$this->ajax_success('编辑成功');
					}
				}
			
		
		}

				$condition = array();
				if($id>0){
					$condition[] = ['ruleid','=',$id];
				}else{
					$condition[] = ['ruleid','=',''];
				}
				$list =  Db::table('tblc_transfer_rule')->where($condition)->first();
				//展示模板
				return view('admin.basiconfig.add_rule',[
				 'action'=>'add_rule',
				 'data'=>$list,
				 'id'=>$id
				]);
	}
	/**
	 * add转账原因
	 **/
	public function add_reason(Request $request) {
		     	$reson_id = $request->input('reson_id');
		    	if($request->isMethod('post')){
				$reson_name = $request->input('reson_name');
				if(!$reson_name){
					$this->ajax_error('转账原因不能为空');
				}

				$data = array(
					'reson_id' => TransferReason::getNextSeq(),
					'reson_name' => $request->input('reson_name'), //转账原因
					'reson_code' => $request->input('reson_code'), //原因编码
					'business_id' => $request->input('business_id'), //业务编号
					'status' => $request->input('status'), //状态
					'remarks' => $request->input('remarks'), //备注

				);

				if(!$reson_id){

					$result = Db::table('tblc_transfer_reson')->insert($data);
					if(!$result){
						$this->ajax_error('添加失败');
					}
						$this->ajax_success('添加成功');
				}else{
					$data['reson_id'] = $reson_id;
					$result1 = Db::table('tblc_transfer_reson')->where('reson_id',$reson_id)->update($data);
					if($result1){
						$this->ajax_success('编辑成功');
					}
				}
			
		
		}
				$condition = array();
				if($reson_id>0){
					$condition[] = ['reson_id','=',$reson_id];
				}else{
					$condition[] = ['reson_id','=',''];
				}
	
				$list =  Db::table('tblc_transfer_reson')->where($condition)->first();
				//展示模板
				return view('admin.basiconfig.add_reason',[
				 'action'=>'add_reason',
				 'data'=>$list,
				 'id'=>$reson_id
				]);
	}
	//删除转账规则
	public function del(Request $request){
		$id = $request->input('id');
		$result = DB::table('tblc_transfer_rule')->where('ruleid',$id)->delete();
		if($result){
			$this->ajax_success('删除成功',$result);
		}
		$this->ajax_error('删除失败',$result);
	}
	//删除转账原因
	public function delreason(Request $request){
		$id = $request->input('id');
		$result = DB::table('tblc_transfer_reson')->where('reson_id',$id)->delete();
		if($result){
			$this->ajax_success('删除成功',$result);
		}
		$this->ajax_error('删除失败',$result);
	}
	


}